from math import sin, cos, pi, sqrt
from Particle import Particle
import sys
import pyglet
import Tkinter as tk

window_width, window_height = 600, 400
twopi = 2 * pi
update_time = 1 / 60.0
radius = 30
color_r, color_g, color_b = 1.0, 1.0, 0
vertices_number = 20
p = Particle(radius, (window_width / 2, window_height / 2), (80.0, 150.0))

if sys.argv[1] == "tk":

    w = tk.Canvas(tk.Tk(), width=window_width, height=window_height, bg='black')
    w.pack()
    particle = w.create_oval(window_width / 2, window_height / 2, radius, radius, outline='yellow')

    def update(dt):
        oldx, oldy = p.x, p.y

        p.move(dt)
        p.bounce((0, window_width, 0, window_height))

        w.move(particle, p.x - oldx, p.y - oldy)
        w.update()
        w.after(17, update, update_time)

    update(0)
    tk.mainloop()

else:
    window = pyglet.window.Window(window_width, window_height)
    fps_display = pyglet.clock.ClockDisplay()

    @window.event
    def on_draw():
        window.clear()

        def circle_vertices():
            delta_angle = twopi / vertices_number
            angle = 0
            while angle < twopi:
                yield p.x + radius * cos(angle)
                yield p.y + radius * sin(angle)
                angle += delta_angle

        pyglet.gl.glColor3f(color_r, color_g, color_b)
        pyglet.graphics.draw(vertices_number, pyglet.gl.GL_LINE_LOOP,
                             ('v2f', tuple(circle_vertices())))

        fps_display.draw()

    def update(dt):
        p.move(dt)
        p.bounce((0, window_width, 0, window_height))

    pyglet.clock.schedule_interval(update, update_time)
    pyglet.app.run()
